﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Upsam.RopaOnline.Modelo.Common;

namespace Upsam.RopaOnline.Web
{
    public partial class CompanySending : System.Web.UI.Page
    {
        private const string PAGE_NAME = "CompanySendingSearch.aspx";
        private const string MANAGEMENT_PAGE_NAME = "CompanySendingManagement.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            // Manda al GridView a resfrescar sus datos contra la base de datos.
            grvCompanySending.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string url = string.Format(CultureInfo.InvariantCulture,
                "{0}?PageMode={1}&IdEntity={2}", MANAGEMENT_PAGE_NAME,
                PageMode.InsertMode, 0);
            Response.Redirect(url, true);
        }

        protected void grvCompanySending_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals(ConstantsEnums.EditItem))
            {
                GridView currentGrid = sender as GridView;
                int idEntity = 0;
                if (currentGrid != null && e.CommandArgument != null &&
                    int.TryParse(currentGrid.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text,
                    out idEntity))
                {
                    string url = string.Format(CultureInfo.InvariantCulture,
                        "{0}?PageMode={1}&IdEntity={2}", MANAGEMENT_PAGE_NAME,
                        PageMode.EditMode, idEntity);
                    Response.Redirect(url, true);
                }
                else
                    throw new ArgumentNullException("Id del registro inválido");
            }
        }
    }
}
